<?php

class Default_Model_User extends Zend_Db_Table{
	
	protected  $_name    = "users";
	protected $_primary = "u_id"; 
	protected $_listSQL;
	
	public function listItems($params = null, $fields = array()){
		$db 	= $this->getAdapter();
		$select = $db->select()->from($this->_name, $fields);
		
		$result = $db->fetchPairs($select);
		// skip params
		echo "<pre>";
		print_r($result);
		echo "</pre>";
		return $result;
	}
	public function loginCheck($params = null){
		if($params['u_username'] != ""){
			$db 	= $this->getAdapter();
			$select = $db->select()->from($this->_name, array("u_username"))
			->where("u_username = ?",$params["u_username"])
			->where("u_password = ?",md5($params["u_password"]));
			$result = $db->fetchRow($select);
			if($result == null) return 0;
		}
		return 1;
	}
	public function getItem($params = null){
		if($params['u_id'] > 0){
			$db 	= $this->getAdapter();
			$select = $db->select()->from($this->_name, array("u_id","u_fullname","u_username","u_password","u_grade","p.p_name"))
						 ->where("u_id = ?",$params["u_id"])
						 ->joinLeft(array('p'=>'province'), 'p.p_id = u_province_id',null);
			
			$result = $db->fetchRow($select);
			
			return $result;
		}
		return null;
	}
	public function addItems($params = null){
		$row = $this->fetchNew();
		$row->u_fullname			= $params['u_fullname'];			
		$row->u_username			= $params['u_username'];			
		$row->u_password			= $params['u_password'];	
		$row->u_province_id			= $params['u_province_id'];						
		$row->u_grade				= $params['u_grade'];						
		$row->save();
	}
// 	public function itemInSelect($arrParam = null,$options = null){
// 		$db = $this->getAdapter();
// 		$select = $db->select()
// 					 ->from($this->_name,array('id','name'));
// 		$result = $db->fetchPairs($select);
// 		return $result;
// 	}
	
// 	public function statusItem($arrParam = null,$options = null){
// 		if($options == null){
			
// 			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';				
// 			$data = array('status'=>$status);
// 			$where = 'id = ' . (int)$arrParam['id'];
// 			$this->update($data, $where);	
// 		}
		
// 		if($options['task'] == 'multi'){
			
// 			$status = $arrParam['s'];				
// 			if(count($arrParam['cid'])>0){
// 				if($status == 'active'){
// 					$ids = implode(',', $arrParam['cid']);
// 					$where = 'id IN (' . $ids . ')';
// 					$data = array('status'=>$status);
// 					$this->update($data, $where);
// 				}else{
// 					foreach ($arrParam['cid'] as $val){						
// 						$where = 'id = ' . $val;
// 						$data = array('status'=>$status);
// 						$this->update($data, $where);						
// 					}
// 				}
// 			}
// 		}
// 	}
	
// 	public function sortItem($arrParam = null,$options = null){
// 		if($options == null){
			
// 			foreach ($arrParam['cid'] as $val){				
// 				$where = 'id = ' . $val;
// 				$data = array('ordering'=>$arrParam['ordering'][$val]);
// 				$this->update($data, $where);
				
// 			}
// 		}
// 	}
	
// 	public function deleteItem($arrParam = null,$options = null){
		
// 		if($options == null){
// 			if((int)$arrParam['id']>0){

// 				$where = 'id = ' . (int)$arrParam['id'];
// 				$this->delete($where);
// 			}
// 		}
// 		if($options['task'] == 'multi'){		
// 			if(count($arrParam['cid'])>0){	
				
// 				foreach ($arrParam['cid'] as $val){
// 					$where = 'id = ' . (int)$val;
// 					$this->delete($where);
// 				}
// 			}
// 		}
// 	}
// 	public function getItem($arrParam = null,$options = null){
// 		/**
// 		 * delete.all
// 		 * ------------------------------------
// 		 * DECODE ID
// 		 */
// 		if(base64_decode($arrParam['id'])){
// 			$arrParam['id'] = base64_decode($arrParam['id']);
// 		}
		
// 		if(empty($arrParam['id'])) return;
// 		if($options == null){
// 			$where = 'id = ' . (int)$arrParam['id'];
// 			$result = $this->fetchRow($where);
// 		}
// 		if($options['task'] == 'info'){
// 			$db = $this->getAdapter();
// 			$sql = $db->select()->from(array('c'=>$this->_name))
// 							->joinLeft(array('u'=>'users'), 'u.id=c.user_id',array('user_name','first_name','last_name','birthday','email'))
// 							->where('c.id = ?',$arrParam['id'],1);
// 			$sql->joinLeft(array('pd'=>'products_cart_detail'), 'c.id=pd.products_cart_id');
// 			$sql->joinLeft(array('p'=>'products_items'), 'pd.products_items_id=p.id',array('pname'=>'p.name'));
// 			$result = $db->fetchRow($sql);		
// 		}
		
// 		if(is_object($result)){
// 			$result = $result->toArray();
// 		}
// 		return $result;
// 	}
	
// 	public function listItem($arrParam = null,$options = null){
// 		if($options == null){
// 			$db = $this->getAdapter();
// 			$select = $db->select()->from(array('i'=>$this->_name));
// 			$select->joinLeft(array('pd'=>'products_cart_detail'), 'i.id=pd.products_cart_id');
// 			$select->joinLeft(array('p'=>'products_items'), 'pd.products_items_id=p.id',array('pname'=>'p.name'));
// 			$select->joinLeft(array('c'=>'cities'), 'p.cities_id=c.id',array('cname'=>'c.name'));
// 			$select->joinLeft(array('u'=>'users'), 'i.user_id=u.id',array('uname'=>'u.user_name'));
// 			$arrFilter = $arrParam['arrFilter'];
// 			if(!empty($arrFilter['col'])){
// 				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
// 			}
			
// 			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
// 				$keyword = '%' . $arrFilter['keyword'] . '%';
// 				$select->where('i.' . $arrFilter['searchField'] . ' LIKE ? ', $keyword);
// 			}
// 			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
// 				$select->where('i.status = ?',$arrFilter['status']);
// 			}
// 			if(!empty($arrFilter['cities_id'])){
// 				$select->where('p.cities_id = ?',$arrFilter['cities_id']);
// 			}
// 			if(!empty($arrFilter['member']) && $arrFilter['member'] != 'none'){
// 				if($arrFilter['member'] == 'member'){
// 					$select->where('i.user_id != ""');
// 				}else{
// 					$select->where('i.user_id = ""');
// 				}				
// 			}
// // 			echo $select;die();
// 			$this->_listSQL = $select;
			
// 			$panigator = $arrParam['panigator'];	
// 			$select->limitPage($panigator['page'], $panigator['rowCount']);
			
// 			$result = $db->fetchAll($select);		 
// 		}else {
// 			$this->_arrParam = $arrParam;
// 			$functionName = $this->call($options['task']);
// 			$result = $this->$functionName();
// 		}
// 		return $result;
// 	}
	
// 	public function totalItem($arrParam = null,$options = null){
		
// 		if($options == null){
// 			$db = $this->getAdapter();
// 			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
// 			$select->columns('COUNT(i.id) AS total');	
// 			//echo '<br>' . $select;
// 			$result = $db->fetchOne($select);
// 		}
// 		if($options['task'] == 'public'){
// 			$db = $this->getAdapter();
// 			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
// 			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
// 			$select->columns('COUNT(id) AS total');	
// 			//echo '<br>' . $select;
// 			$result = $db->fetchOne($select);
// 		}
// 		return $result;
		
// 	}
	
// 	public function saveItem($arrParam = null,$options = null){
		
// 		$function  = Zendvn_System_Function::call($options['task']);
// 		$this->$function($arrParam);
// 	}
	
// 	public function adminAdd($arrParam = null){
			
// 		$row = $this->fetchNew();
// 		$row->full_name				= $arrParam['full_name'];			
// 		$row->email					= $arrParam['email'];			
// 		$row->phone					= $arrParam['phone'];	
// 		$row->address				= $arrParam['address'];						
// 		$row->shipping				= $arrParam['shipping'];						
// 		$row->comment				= $arrParam['comment'];						
// 		$row->status				= $arrParam['status'];						
// 		$row->ip					= $this->getIp();	
// 		$row->active_code			= md5($arrParam['email'] . time());
// 		$row->save();
// 	}
	
// 	public function adminEdit($arrParam = null){
// 		$where  = 'id = ' . (int)$arrParam['id'];
// 		$row = $this->fetchRow($where);
// 		$row->status				= $arrParam['status'];						
// 		$row->save();
// 	}
	
// 	public function publicAdd($arrParam = null){
// 		$ssInfo = new Zendvn_System_Info();
// 		$info 	= $ssInfo->getMemberInfo();
		
// 		/**
// 		 * delete.all
// 		 * ------------------------------------
// 		 * TH1: Dia chi thanh toan la dia chi giao hang
// 		 */
// 		$db		= $this->getAdapter();
		
// 		if($arrParam['check1'] == 'on'){
// 			$bind['full_name']			= $arrParam['txtName_0'];			
// 			$bind['phone']				= $arrParam['txtPhone_0'];	
// 			$bind['address']			= $arrParam['txtDiachi_0'];		
// 			$bind['comment']			= $arrParam['txtDesc_0'];						
// 			$bind['ip']					= $this->getIp();	
// 			$bind['active_code']		= md5($info['email'] . time());
// 		}
// 		if($arrParam['check2'] == 'on'){
// 			$bind['full_name']			= $arrParam['txtName_1a'];			
// 			$bind['phone']				= $arrParam['txtPhone_1a'];	
// 			$bind['address']			= $arrParam['txtDiachi_1a'];			
// 			$bind['comment']			= $arrParam['txtDesc_1a'];						
// 			$bind['ip']					= $this->getIp();	
// 			$bind['active_code']		= md5($info['email'] . time());
// 			if($arrParam['cbGift_1'] == 'on'){
// 				$shipping['name'] 		= $arrParam['txtName_1b'];
// 				$shipping['phone'] 		= $arrParam['txtPhone_1b'];
// 				$shipping['address'] 	= $arrParam['txtDiachi_1b'];
// 				$shipping['comment'] 	= $arrParam['txtDesc_1b'];
// 				$bind['shipping']		= Zend_Json::encode($shipping);;
// 			}
// 		}
// 		if($arrParam['check3'] == 'on'){
// 			$bind['full_name']			= $arrParam['txtName_2'];			
// 			$bind['phone']				= $arrParam['txtPhone_2'];	
// 			$bind['address']			= $arrParam['txtDiachi_2'];			
// 			$bind['comment']			= $arrParam['txtDesc_2'];						
// 			$bind['ip']					= $this->getIp();	
// 			$bind['active_code']		= md5($info['email'] . time());
// 		}
// 		if($arrParam['check4'] == 'on'){
// 			$bind['full_name']			= $arrParam['txtName_3a'];			
// 			$bind['phone']				= $arrParam['txtPhone_3a'];	
// 			$bind['address']			= $arrParam['txtDiachi_3a'];
// 			$bind['comment']			= $arrParam['txtDesc_3a'];						
// 			$bind['ip']					= $this->getIp();	
// 			$bind['active_code']		= md5($info['email'] . time());
// 			if($arrParam['cbGift_3'] == 'on'){
// 				$shipping['name'] 		= $arrParam['txtName_3b'];
// 				$shipping['phone'] 		= $arrParam['txtPhone_3b'];
// 				$shipping['comment'] 	= $arrParam['txtDesc_3b'];
// 				$shipping['address'] 	= $arrParam['txtDiachi_3a'];
// 				$bind['shipping']		= Zend_Json::encode($shipping);;
// 			}
// 		}
// 		if($arrParam['check5'] == 'on'){
// 			$bind['billing'] = Zend_Json::encode(array(
// 					'bill_fullname' 	=> $arrParam['txtName_5a'],
// 					'bill_phone' 		=> $arrParam['txtPhone_5a'],
// 					'bill_address' 		=> $arrParam['txtDiachi_5a'],
// 					'bill_note' 		=> $arrParam['txtDesc_5a'],
// 			));
// 			$bind['shipping'] = Zend_Json::encode(array(
// 					'ship_fullname' 	=> $arrParam['txtName_5b'],
// 					'ship_phone' 		=> $arrParam['txtPhone_5b'],
// 					'ship_address' 		=> $arrParam['txtDiachi_5b'],
// 					'ship_note' 		=> $arrParam['txtDesc_5b'],
// 			));
// 		}else{
// 			$bind['billing'] = Zend_Json::encode(array(
// 					'bill_fullname' 	=> $arrParam['txtName_5'],
// 					'bill_phone' 		=> $arrParam['txtPhone_5'],
// 					'bill_address' 		=> $arrParam['txtDiachi_5'],
// 					'bill_note' 		=> $arrParam['txtDesc_5'],
// 			));
// 			$bind['shipping'] = Zend_Json::encode(array(
// 					'ship_fullname' 	=> $arrParam['txtName_5'],
// 					'ship_phone' 		=> $arrParam['txtPhone_5'],
// 					'ship_address' 		=> $arrParam['txtDiachi_5'],
// 					'ship_note' 		=> $arrParam['txtDesc_5'],
// 			));
// 		}
// 		$bind['user_id'] = $ssInfo->getMemberInfo('id');
// 		$bind['active_code']		= md5($info['email'] . time());
// 		$bind['ip']					= $this->getIp();
// 		$bind['total']				= $arrParam['price']*$arrParam['ddlSL_0'];
// 		$db->insert($this->_name, $bind);   
		
// 		return $db->lastInsertId($this->_name,'id');
// 	}
	
	
// 	private function getIp() { 
// 		if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet 
// 			$ip=$_SERVER['HTTP_CLIENT_IP']; 
// 		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //to check ip is pass from proxy 
// 				$ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
// 			} else { 
// 				$ip=$_SERVER['REMOTE_ADDR']; 
// 			} 
// 		return $ip;
// 	}

}